package iot.ht.controller.htcls;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import iot.core.annotation.Auth;
import iot.core.annotation.Log;
import iot.core.data.BasePageDTQto;
import iot.core.data.IdNameDto;
import iot.core.data.PageData;
import iot.core.data.entity.HTCls;
import iot.core.data.mapper.HTClsMapper;
import iot.core.enums.AuthType;
import iot.ht.controller.htuser.data.HTUserDto;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.lang.reflect.InvocationTargetException;

@Slf4j
@RestController
@RequestMapping("/htcls")
@Tag(name = "类型管理", description = "")
public class HTClsController {
    @Autowired
    HTClsMapper htClsMapper;

    @Auth(authType = AuthType.Login)
    @Log(logNeed = false,logTemplete ="",logPin = true,logPout = true)
    @PostMapping("/page")
    @Operation(summary = "类型分页", description = "")
    public PageData<HTCls> page(@RequestBody BasePageDTQto qm) throws Exception {
        var qw=htClsMapper.getQueryWrapper();
        if(StringUtils.hasText(qm.getDTB() )){
            qw.ge("Edt_DT",qm.getDB() );
        }

        if(StringUtils.hasText(qm.getDTE() )){
            qw.lt("Edt_DT",qm.getDE() );
        }

        if(StringUtils.hasText(qm.getKey() )){
            qw.like("FullName",qm.getKey() );
        }
        var sort=qm.getSort();
        qw.orderBy(true,sort.isIsAsc(),sort.getFiled() );



        var r=htClsMapper.page( qw,qm );
        return r;
    }


    @Auth(authType = AuthType.Login)
    @Log(logNeed = false,logTemplete ="",logPin = true,logPout = true)
    @PostMapping("/newobj")
    @Operation(summary = "根据类名创建对象", description = "")
    public Object NewObj(@RequestBody IdNameDto qm) throws ClassNotFoundException, InstantiationException, IllegalAccessException, NoSuchMethodException, InvocationTargetException {

        Class<?> clazz = Class.forName(qm.getName());
        Object r= clazz.getDeclaredConstructor().newInstance();
        return r;
    }
}
